#include "GLSLESGpuProgram.h"
#include "GLSLESProgram.h"
#include "GLSLESLinkProgramManager.h"

namespace PVM
{
	GLuint GLSLESGpuProgram::mVertexShaderCount = 0;
	GLuint GLSLESGpuProgram::mFragmentShaderCount = 0;
	
	GLSLESGpuProgram::GLSLESGpuProgram(GLSLESProgram* parent) :
		GLSLESProgram(parent->getCreator(), parent->getName(), parent->getHandle(),
					  parent->getGroup(), false, 0), mGLSLProgram(parent)
	{
		mType = parent->getType();
		mSyntaxCode = "glsles";
		
		if (parent->getType() == GPT_VERTEX_PROGRAM)
		{
			mProgramID = ++mVertexShaderCount;
		}
		else if (parent->getType() == GPT_FRAGMENT_PROGRAM)
		{
			mProgramID = ++mFragmentShaderCount;
		}
		
		mSkeletalAnimation = mGLSLProgram->isSkeletalAnimationIncluded();
		mLoadFromFile = false;
	}
	
	GLSLESGpuProgram::~GLSLESGpuProgram()
	{
		unload();
	}
	
	void GLSLESGpuProgram::loadImpl(void)
	{
	}
	
	void GLSLESGpuProgram::unloadImpl(void)
	{
	}
	
	void GLSLESGpuProgram::loadFromSource(void)
	{
	}
	
	void GLSLESGpuProgram::bindProgram(void)
	{
		switch(mType)
		{
			case GPT_VERTEX_PROGRAM:
				GLSLESLinkProgramManager::getSingleton().setActiveVertexShader(this);
				break;
			case GPT_FRAGMENT_PROGRAM:
				GLSLESLinkProgramManager::getSingleton().setActiveFragmentShader(this);
				break;
			case GPT_GEOMETRY_PROGRAM:
			default:
				break;
		}
	}
	
	void GLSLESGpuProgram::unbindProgram(void)
	{
		if (mType == GPT_VERTEX_PROGRAM)
		{
			GLSLESLinkProgramManager::getSingleton().setActiveVertexShader(NULL);
		}
		else if (mType == GPT_FRAGMENT_PROGRAM)
		{
			GLSLESLinkProgramManager::getSingleton().setActiveFragmentShader(NULL);
		}
	}
	
	void GLSLESGpuProgram::bindProgramParameters(GpuProgramParametersSharedPtr params, uint16 mask)
	{
		try
		{
			GLSLESLinkProgram* linkProgram = GLSLESLinkProgramManager::getSingleton().getActiveLinkProgram();
			linkProgram->updateUniforms(params, mask, mType);
		}
		catch (Exception& e) {}
	}
	
	void GLSLESGpuProgram::bindProgramPassIterationParameters(GpuProgramParametersSharedPtr params)
	{
		GLSLESLinkProgram* linkProgram = GLSLESLinkProgramManager::getSingleton().getActiveLinkProgram();
		linkProgram->updatePassIterationUniforms(params);
	}
}
